package com.itheima.zixi6.TCP;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        //1.创建ServerSocket对象，并且指定绑定的端口
        ServerSocket ss = new ServerSocket(8080);
        //2.调用accept方法，等待客户端连接请求
        Socket socket = ss.accept();
        //3.从socket通信管道中得到一个字节输入流
        InputStream is = socket.getInputStream();
        //4.把字节输入流转换成数据输入流
        DataInputStream dis = new DataInputStream(is);
        //5.读取客户端发送的数据
        while (true) {
            try {
                String data = dis.readUTF();
                System.out.println("服务器接收到的数据：" + data);
                System.out.println(socket.getRemoteSocketAddress());
            } catch (IOException e) {
                System.out.println(socket.getRemoteSocketAddress()+"已经离线了");
                socket.close();
                dis.close();
                break;
            }
            //6.无限接收数据不需要关闭流和socket，释放资源
        }

    }
}


